package com.shangguan.springboot.realm;

import com.shangguan.springboot.dao.TPermissionsMapper;
import com.shangguan.springboot.dao.TRoleMapper;
import com.shangguan.springboot.dao.TUserMapper;
import com.shangguan.springboot.entity.TPermissions;
import com.shangguan.springboot.entity.TRole;
import com.shangguan.springboot.entity.TUser;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @ClassName: UserRealm
 * @Description: 用户关联
 * @Author: 一尘
 * @Date: 2020 年 03 月 09 21:00
 * @Version 1.0
 */
@Slf4j
public class UserRealm extends AuthorizingRealm {
    @Autowired
    TUserMapper tUserMapper;
    @Autowired
    TRoleMapper tRoleMapper;
    @Autowired
    TPermissionsMapper tPermissionsMapper;
     // 验证
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        log.info("----------------------开始验证授权------------------");
        //获取用户信息
      String name = (String)  principalCollection.getPrimaryPrincipal();
      //根据用户信息查询数据
        TUser user = tUserMapper.selectByName(name) ;
        //添加角色和权限
        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
        for (TRole role : tRoleMapper.selectRoleByTuserName(name)) {
            //添加角色
            simpleAuthorizationInfo.addRole(role.getRoleName());
            //添加权限
            for (TPermissions permissions : tPermissionsMapper.findTpermissionsByRoleId(role.getRoleId())) {
                simpleAuthorizationInfo.addStringPermission(permissions.getPermissionsName());
            }
        }
        log.info("------------授权---------"+simpleAuthorizationInfo);
        return simpleAuthorizationInfo;
    }
    //授权
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        log.info("------------执行认证逻辑-------------");
        //加这一步的目的是在Post请求的时候会先进认证，然后在到请求
        if (authenticationToken.getPrincipal() == null) {
            return null;
        }
        //获取用户信息
        String name = authenticationToken.getPrincipal().toString();
        TUser user = tUserMapper.selectByName(name);
        log.info("==========================="+user.getUserPassword());
        if (user == null) {
            //这里返回后会报出对应异常
            log.error("================= user  is null");
            //throw new AccountException("Null usernames are not allowed by this realm.");
            return null;
        } else {
            //这里验证authenticationToken和simpleAuthenticationInfo的信息
            log.info("----------------------验证用户信息----------------------");
            SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(name, user.getUserPassword(), getName());
            log.info("-----------------------------------------------------------------"+simpleAuthenticationInfo);
            return simpleAuthenticationInfo;
        }
    }
}
